package com.xmg.server.goods.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.xmg.server.goods.mapper.ProductMapper;
import com.xmg.shop.api.domain.Product;
import com.xmg.shop.api.domain.ProductCatalogPropertyValue;
import com.xmg.shop.api.domain.ProductDesc;
import com.xmg.shop.api.domain.ProductImage;
import com.xmg.shop.api.page.PageResult;
import com.xmg.shop.api.query.ProductQueryObject;
import com.xmg.shop.api.service.*;
import com.xmg.shop.api.vo.ProductVo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

@Service
public class ProductServiceImpl implements IProductService {
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private IProductDescService productDescService;
    @Autowired
    private IProductImageService productImageService;
    @Autowired
    private IProductCatalogPropertyValueService productCatalogPropertyValueService;
    @Autowired
    private IBrandService brandService;
    @Autowired
    private ICatalogService catalogService;

    @Override
    public Product getProduct(Long id) {
        return productMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageResult queryProduct(ProductQueryObject qo) {
        Long count = productMapper.queryPageCount(qo);
        if (count <= 0) {
            return PageResult.empty(qo.getPageSize());
        }
        List<Product> listData = productMapper.queryPageData(qo);
        return new PageResult(listData, count.intValue(), qo.getCurrentPage(), qo.getPageSize());
    }

    @Override
    public List<Product> selectAll() {
        return productMapper.selectAll();
    }

    @Override
    public Product get(Long id) {
        return this.productMapper.selectByPrimaryKey(id);
    }

    @Override
    public void update(Product product) {
        productMapper.update(product);
    }

    @Override
    public Product save(ProductVo vo) {

        Product product = vo.getProduct();
        if (product != null) {
            productMapper.insert(product);
        }

        ProductDesc productDesc = vo.getProductDesc();
        if (product.getId() != null && productDesc != null) {
            productDesc.setProductId(product.getId());
            productDescService.save(productDesc);

        }

        List<ProductImage> productImages = vo.getProductImages();
        if (productImages != null) {
            for (ProductImage productImage : productImages) {
                productImage.setProductId(product.getId());
                if ((productImage.getImagePath() != null) && (!productImage.getImagePath().equals(""))) {
                    productImageService.save(productImage);
                }
            }
        }

        List<ProductCatalogPropertyValue> propertys = vo.getPropertys();
        if (propertys != null) {
            for (ProductCatalogPropertyValue productCatalogPropertyValue : propertys) {
                productCatalogPropertyValue.setProductId(product.getId());
                productCatalogPropertyValueService.save(productCatalogPropertyValue);
            }
        }
        return product;
    }
}
